package com.andaily.web.controller.mobile.account.statistics;

import com.andaily.domain.dto.account.AccountDailyChartDto;
import com.andaily.domain.dto.account.AccountMonthChartDto;
import com.andaily.domain.dto.account.AccountPaymentsStatisticsDto;
import com.andaily.domain.dto.account.AccountTypeChartDto;
import com.andaily.infrastructure.support.LogHelper;
import com.andaily.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;

/**
 * @author Shengzhao Li
 */
@Controller
@RequestMapping("/account_statistics/chart")
public class AccountStatisticsChartController {

    /**
     * Logger
     */
    private static LogHelper log = LogHelper.create(AccountStatisticsChartController.class);

    @Autowired
    private AccountService accountService;

    // Just show max amount total 30 records
    @RequestMapping("type")
    public String typeChart(Model model) {
        AccountTypeChartDto typeChartDto = accountService.loadTypeChartData();
        model.addAttribute("typeChartDto", typeChartDto);
        return "account/statistics/account_type_chart";
    }

    // Just show latest 30  days records
    @RequestMapping("daily")
    public String dailyChart(Model model) {
        AccountDailyChartDto dailyChartDto = accountService.loadDailyChartData();
        model.addAttribute("dailyChartDto", dailyChartDto);
        return "account/statistics/account_daily_chart";
    }

    // Just show latest 18 months records
    @RequestMapping("month")
    public String monthChart(Model model) {
        AccountMonthChartDto monthChartDto = accountService.loadMonthChartData();
        model.addAttribute("monthChartDto", monthChartDto);
        return "account/statistics/account_month_chart";
    }

    @RequestMapping("payments")
    public String payments(Model model) {
        AccountPaymentsStatisticsDto paymentsStatisticsDto = accountService.loadAccountPaymentsStatisticsDto();
        model.addAttribute("paymentsStatisticsDto", paymentsStatisticsDto);
        return "account/statistics/account_payments_chart";
    }

    @RequestMapping("percent")
    public String percent(BigDecimal total, BigDecimal self, Model model) {
        if (total == null) {
            total = BigDecimal.ZERO;
            self = BigDecimal.ZERO;
        }
        if (self == null) {
            self = BigDecimal.ZERO;
        }

        model.addAttribute("other", total.subtract(self));
        model.addAttribute("self", self);
        return "account/statistics/account_percent_chart";
    }

}
